package com.lyz.myself.service.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyz.myself.mapper.PositionMapper;
import com.lyz.myself.mapper.RoleMapper;
import com.lyz.myself.mapper.UserMapper;
import com.lyz.myself.pojo.Po.Position;
import com.lyz.myself.pojo.Po.Role;
import com.lyz.myself.pojo.Po.User;
import com.lyz.myself.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Resource
    private UserMapper userMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User getUser(String userName) {
        return userMapper.getUser(userName);
    }

    @Override
    public List<Role> getRole(Integer user_id) {
        return userMapper.getRole(user_id);
    }

    @Override
    public String getPassword(String userName) {
        return userMapper.getPassword(userName);
    }

    @Override
    public PageInfo<User> getAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.getAll();
        PageInfo<User> page = new PageInfo<>(userList);
        return page;
    }

    @Override
    public List<Position> getPosition(Integer position_id) {
        return userMapper.getPosition(position_id);
    }

    @Override
    public User selectByPrimaryKey(Integer user_id) {
        return userMapper.selectByPrimaryKey(user_id);
    }

    /**
     * 查询所有用户的姓名手机号
     */
    @Override
    public PageInfo<Map<String, String>> getAllUser(Integer company_id, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> userList = userMapper.getAllUser(company_id);
        PageInfo<Map<String, String>> page = new PageInfo<>(userList);
        return page;
    }

    /**
     * 查看个人信息
     *
     * @param user_id
     * @return
     */
    public Map<String, String> fidMeInformation(Integer user_id, Integer company_id) {
        return userMapper.fidMeInformation(user_id, company_id);
    }

    @Override
    public PageInfo<Map<String, String>> myUser(Integer company_id, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> userList = userMapper.myUser(company_id);
        PageInfo<Map<String, String>> page = new PageInfo<>(userList);
        return page;
    }

    @Override
    public Integer insertSelective(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public Integer updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getUserByCompany(String userName, Integer company_id) {
        return userMapper.getUserByCompany(userName, company_id);
    }

    /**
     * 维达绑定crm账号
     *
     * @param map
     * @return
     */
    @Override
    public Integer bindTheUser(Map<String, Object> map) {
        return userMapper.bindTheUser(map);
    }

    /**
     * 通过维达账号判断是否绑定
     *
     * @param vaderName
     * @return
     */
    @Override
    public User findTheUser(String vaderName) {
        return userMapper.findTheUser(vaderName);
    }

    /**
     * 删除超级管理员
     *
     * @param list
     * @return
     */
    @Transactional
    @Override
    public boolean deleteAdmin(List<Integer> list) {
        Map<String, Object> listRoleId = userMapper.getUserRoleId(list.get(0).toString());
        int deleteRole = roleMapper.deleteRole((Integer) listRoleId.get("role_id"));
        int delete = userMapper.deleteAdmin(list);
        boolean boo ;
        if (delete > 0 && listRoleId != null && deleteRole > 0) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }


    /**
     * 查询role_id
     *
     * @param user_id
     * @return
     */
    @Override
    public Map<String, Object> getUserRoleId(String user_id) {
        return userMapper.getUserRoleId(user_id);
    }


    /**
     * 解绑维达账号
     * @param userId
     * @return
     */
    @Override
    public String unbundlingUser(Integer userId){
        return userMapper.unbundlingUser(userId);
    }
}
